﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommHelper
{
    public class TransData
    {
        public static string Tran(string value)
        {
            if (value == null)
            {
                value = "";
            }
            else
            {
                value = value.Replace("+", "%2b")
                        .Replace("=", "%3d")
                        .Replace("&", "%26")
                        .Replace("'", "$*$*$")
                        .Replace("\\", "$!$!$")
                        .Replace("\"", "$@$@$");
            }
            return value;
        }

        public string Data
        {
            get
            {
                string temp = "[{";
                foreach (KeyValuePair<string, string> kvp in kvs)
                {
                    temp += "\\\"" + kvp.Key + "\\\":\\\"" + kvp.Value + "\\\",";
                }
                temp = temp.TrimEnd(',');
                temp += "}]";

                return temp;
            }
        }

        private readonly Dictionary<string, string> kvs = new Dictionary<string, string>();

        public void AddKey(string key, string value)
        {
            if (value == null)
            {
                value = "";
            }
            else
            {
                value = value.Replace("+", "%2b")
                        .Replace("=", "%3d")
                        .Replace("&", "%26")
                        .Replace("'", "$*$*$")
                        .Replace("\\", "$!$!$")
                        .Replace("\"", "$@$@$") ;
            }
            if (!key.Contains(key))
            {
                kvs.Add(key, value);
            }
            else
            {
                kvs[key] = value;
            }

        }
    }
}
